package dailytest;

import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.util.Objects;
import java.util.Optional;

/**
 * @author: heweiwei
 * @Date: 2023/11/17 18:28
 * @Desc:
 */
public class DispenseSummaryFormDetailSorter {

    public static int sort(BaseDispenseSummaryFormDetail a, BaseDispenseSummaryFormDetail b) {

        String cabinetCargoSpace1 = null;

        String cabinetCargoSpace2 = null;

        if (Objects.nonNull(a.getCabinetName())) {
            cabinetCargoSpace1 = a.getCabinetName().concat("-").concat(Optional.ofNullable(a.getCargoSpaceNo()).orElse(""));
        }
        if (Objects.nonNull(b.getCabinetName())) {
            cabinetCargoSpace2 = b.getCabinetName().concat("-").concat(Optional.ofNullable(b.getCargoSpaceNo()).orElse(""));
        }

        boolean cabinetCargoSpace1IsEmpty = StringUtils.isEmpty(cabinetCargoSpace1);
        boolean cabinetCargoSpace2IsEmpty = StringUtils.isEmpty(cabinetCargoSpace2);


        if (cabinetCargoSpace1IsEmpty && cabinetCargoSpace2IsEmpty) {
            return Optional.ofNullable(a.getMedicationName()).orElse("").compareTo(Optional.ofNullable(b.getMedicationName()).orElse(""));
        } else if (cabinetCargoSpace1IsEmpty) {
            //为空排后面
            return 1;
        } else if (cabinetCargoSpace2IsEmpty) {
            //为空排后面
            return -1;
        } else {

            int compareResult = cabinetCargoSpace1.compareTo(cabinetCargoSpace2);

            return compareResult != 0 ? compareResult : a.getMedicationName().compareTo(b.getMedicationName());
        }
    }

    @Data
    public static class BaseDispenseSummaryFormDetail {
        private String cabinetName;

        private String cargoSpaceNo;

        private String medicationName;
    }





}
